﻿namespace IntegrationTests.SchemaParsingTests
{
    using System;
    using System.Diagnostics;
    using System.IO;
    using System.Text.RegularExpressions;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    [TestClass]
    public class SyntaxParserTests
    {
        private const string ADSchemaFilePath =
                @"C:\Users\administrator.FABRIKAM\Documents\Visual Studio 2013\Projects\ADSSIS\V2\IntegrationTests\SchemaParsingTests\ADRowSchema.txt";

        private const string EdirectoryFilePath =
            @"C:\Users\administrator.FABRIKAM\Documents\Visual Studio 2013\Projects\ADSSIS\V2\IntegrationTests\SchemaParsingTests\EDirectorySchema.txt";

        private const string SyntaxRegexPattern = 
                @"(?<syntaxLabel>\bSYNTAX\b)\s+(?<syntaxValue>'?\d\.[\d.]+)";

        private const string NameRegexPattern = @"(?<nameLabel>\bNAME\b)\s+(?<nameValue>'[\w|-]+')";
        // example "( 2.5.4.4 NAME ( 'sn' 'surname' ) SYNTAX 1.3.6.1.4.1.1466.115.121.1.15{64} X-NDS_NAME 'Surname' X-NDS_LOWER_BOUND '1' X-NDS_UPPER_BOUND '64' X-NDS_PUBLIC_READ '1' X-NDS_NONREMOVABLE '1' )";
        private const string NameRegexForSynonims = @"(?<nameLabel>\bNAME\b)\s+(?<nameValue>\(\s+'[\w|-]+'\s+'[\w|-]+'\s\))";

        private readonly Regex nameRegex = new Regex(NameRegexPattern);
        private readonly Regex nameRegexWithSynonims = new Regex(NameRegexForSynonims);

        [TestMethod]
        public void NameRegexTest()
        {
            CheckSchemaFile(ADSchemaFilePath);
            CheckSchemaFile(EdirectoryFilePath);
        }

        private void CheckSchemaFile(string fileName)
        {
            using (var sr = new StreamReader(fileName))
            {
                String line;
                while ((line = sr.ReadLine()) != null)
                {
                    var match = nameRegex.Match(line);
                    if (!match.Success)
                    {
                        var matchForSynonims = nameRegexWithSynonims.Match(line);
                        Debug.WriteLine(line);
                        Debug.WriteLine(matchForSynonims.Groups[2].Value);
                        Assert.IsTrue(matchForSynonims.Success);
                    }
                    else
                    {
                        Debug.WriteLine(line);
                        Debug.WriteLine(match.Groups[2].Value);
                        Assert.IsTrue(match.Success);
                    }
                }
            }
        }

        [TestMethod]
        public void SynaxRegexTestAD()
        {
            var syntaxRegex = new Regex(SyntaxRegexPattern);
            using (var sr = new StreamReader(ADSchemaFilePath))
            {
                String line;                
               while ((line = sr.ReadLine()) != null)
               {
                   var match = syntaxRegex.Match(line);
                   // Debug.WriteLine(line);
                   // Debug.WriteLine(match.Groups[2].Value);

                   // 2 attributes in AD repsFrom and repsTo have OctetString as an attribute Syntax, not usre if this valid
                   // Ignoring them for now
                   if (!line.Contains("OctetString"))  
                   {
                       Assert.IsTrue(match.Success);
                   }
              }
          }
        }

        [TestMethod]
        public void SynaxRegexTestEDirectoryTest()
        {
            var syntaxRegex = new Regex(SyntaxRegexPattern);
            using (var sr = new StreamReader(EdirectoryFilePath))
            {
                String line;
                while ((line = sr.ReadLine()) != null)
                {
                    var match = syntaxRegex.Match(line);
                    Debug.WriteLine(line);
                    Debug.WriteLine(match.Groups[2].Value);
                    Assert.IsTrue(match.Success);
                }
            }
        }
    }
}
